package org.zizi;

import org.apache.jasper.servlet.JspServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.thread.QueuedThreadPool;

public abstract class ServerBase
{
	private Server server = new Server();
	
	private int threads = 500;
	private int port = 8080;
	private int acceptors = 2;
	private int queue = 1000;
	private int confidential = 8443;
	private String context = "";
	private String resource = "www";
	
	protected ServerBase()
	{
	}
	
	protected void init()
	{
		QueuedThreadPool threadPool = new QueuedThreadPool();
		threadPool.setMaxThreads(threads);
		server.setThreadPool(threadPool);

		SelectChannelConnector connector1 = new SelectChannelConnector();
		connector1.setPort(port);
		connector1.setAcceptors(acceptors);
		connector1.setAcceptQueueSize(queue);
		connector1.setConfidentialPort(confidential);
		server.addConnector(connector1);

		ServletHolder servlet = new ServletHolder(new JspServlet());
		servlet.setInitParameter("fork", "false");
		servlet.setInitOrder(100);

		WebAppContext context = new WebAppContext();
		context.setContextPath(this.context);
		context.setResourceBase(resource);
		context.addServlet(servlet, "/a.jsp");
		server.addHandler(context);

		server.setStopAtShutdown(true);
		server.setSendServerVersion(true);
	}

	protected void setServer(Server server)
	{
		this.server = server;
	}

	protected void setThreads(int threads)
	{
		this.threads = threads;
	}

	protected void setPort(int port)
	{
		this.port = port;
	}

	protected void setAcceptors(int acceptors)
	{
		this.acceptors = acceptors;
	}

	protected void setQueue(int queue)
	{
		this.queue = queue;
	}

	protected void setConfidential(int confidential)
	{
		this.confidential = confidential;
	}

	protected void setContext(String context)
	{
		this.context = context;
	}

	protected void setResource(String resource)
	{
		this.resource = resource;
	}

	protected void run() throws Exception
	{
		server.start();
		server.join();
	}
}
